package com.molichuxing.gateway.bff.goods.convertor;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.gateway.bff.basic.entity.response.VehicleMakeVo;
import com.molichuxing.gateway.bff.goods.entity.response.*;
import com.molichuxing.gateway.bff.orders.entity.response.CostDetailsVo;
import com.molichuxing.gateway.bff.orders.entity.response.DataVo;
import com.molichuxing.gateway.contants.OrderConst;
import com.molichuxing.gateway.property.OrderCreateTypeEnum;
import com.molichuxing.gateway.property.PowerTypeEnum;
import com.molichuxing.services.business.dto.response.*;
import com.molichuxing.services.infrastructure.dto.response.GoodsIntroduceDto;
import com.molichuxing.services.infrastructure.dto.response.VehicleDto;
import com.molichuxing.services.infrastructure.dto.response.VehicleMakeDto;
import com.molichuxing.services.infrastructure.dto.response.VehicleModelParamDto;
import com.molichuxing.services.property.GoodsPaymentTypeEnum;
import com.molichuxing.services.property.GoodsTypeEnum;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @prouect_name: op-gateway
 * @class_name: ${CLASS_NAME}
 * @description: ${DESCRIPTION}
 * @author: 林亿建
 * @create_date: 2019年08月01日
 * @modify_date: 2019年08月01日
 **/
public class Convert {
	/********************************************
	 * 商品属性信息转换 开始
	 *****************************/
	public static Paged<GoodsVo> toGoodsPage(Paged<GoodsBizBizDto> page, Map<Integer, VehicleDto> vehicleMap,
											 Map<Integer, String> vehicleImgMap, Map<Integer, List<VehicleModelParamDto>> vehicleParamMap) {
		Paged<GoodsVo> result = null;
		;
		if (null != page) {
			result = new Paged<>();
			BeanUtils.copyProperties(page, result);
			result.setList(toGoodsVo(page.getList(), vehicleMap, vehicleImgMap, vehicleParamMap));
		}
		return result;
	}

	public static List<GoodsVo> toGoodsVo(List<GoodsBizBizDto> beanList, Map<Integer, VehicleDto> vehicleMap,
										  Map<Integer, String> vehicleImgMap, Map<Integer, List<VehicleModelParamDto>> vehicleParamMap) {
		List<GoodsVo> dtoList = null;
		;
		if (beanList != null && beanList.size() > 0) {
			dtoList = new ArrayList<>();
			for (GoodsBizBizDto bean : beanList) {
				dtoList.add(toGoodsVo(bean, vehicleMap, vehicleImgMap, vehicleParamMap));
			}
		}
		return dtoList;
	}

	public static GoodsVo toGoodsVo(GoodsBizBizDto bean, Map<Integer, VehicleDto> vehicleMap,
									Map<Integer, String> vehicleImgMap, Map<Integer, List<VehicleModelParamDto>> vehicleParamMap) {
		GoodsVo beanDto = null;
		if (bean != null) {
			beanDto = new GoodsVo();
			BeanUtils.copyProperties(bean, beanDto);
			StringBuffer sb = new StringBuffer();
			if (null != vehicleMap && (null != vehicleMap.get(bean.getGoodsId()))) {
				beanDto.setVehicleStyleStr(vehicleMap.get(bean.getGoodsId()).getName());
				sb.append(PowerTypeEnum.getName(vehicleMap.get(bean.getGoodsId()).getPowerType()));
			}
			if (null != vehicleImgMap && (null != vehicleImgMap.get(bean.getGoodsId()))) {
				beanDto.setModelImg(vehicleImgMap.get(bean.getGoodsId()));
			}
			if (null != vehicleParamMap && (null != vehicleParamMap.get(bean.getGoodsId()))) {
				getVehicleParam(vehicleParamMap.get(bean.getGoodsId()), sb);
			}
			beanDto.setVehicleInfo(sb.toString());
		}
		return beanDto;
	}

	public static GoodsAppDetailVo toGoodsAppDetailVo(GoodsOrderBizDto bean, VehicleDto vehicle, List<VehicleModelParamDto> vehicleParamList) {
		GoodsAppDetailVo beanDto = null;
		if (bean != null) {
			beanDto = new GoodsAppDetailVo();
			BeanUtils.copyProperties(bean, beanDto);
			StringBuffer sb = new StringBuffer();
			if (null != vehicle) {
				beanDto.setVehicleStyleStr(vehicle.getName());
				sb.append(PowerTypeEnum.getName(vehicle.getPowerType()));
			}
			getVehicleParam(vehicleParamList, sb);
			beanDto.setVehicleInfo(sb.toString());
			composeAppGoods(beanDto);
			beanDto.setThirdLookCarLink(vehicle.getVrPicUrl());
		}
		return beanDto;
	}

	public static void getVehicleParam(List<VehicleModelParamDto> vehicleParamList, StringBuffer sb) {
		if (null != vehicleParamList && (vehicleParamList.size() > 0)) {
			if (null != vehicleParamList && vehicleParamList.size() == 2) {
				sb.append(" · ");
//				sb.append("/续航");
				sb.append(vehicleParamList.get(0).getParamValue());
				sb.append("公里");
				sb.append(" · 快充");
//				sb.append((vehicleParamList.get(1).getParamValue().split("min"))[0]);
//				sb.append("分钟");
			}
		}
	}

	public static GoodsAppConfirmDetailVo toGoodsAppConfirmDetailVo(GoodsOrderBizDto bean, VehicleDto vehicle, List<VehicleModelParamDto> vehicleParamList) {
		GoodsAppConfirmDetailVo beanDto = null;
		if (bean != null) {
			beanDto = new GoodsAppConfirmDetailVo();
			BeanUtils.copyProperties(bean, beanDto);
			StringBuffer sb = new StringBuffer();
			if (null != vehicle) {
				beanDto.setVehicleStyleStr(vehicle.getName());
				sb.append(PowerTypeEnum.getName(vehicle.getPowerType()));
			}
			getVehicleParam(vehicleParamList, sb);
			beanDto.setVehicleInfo(sb.toString());
			if (bean.getGoodsType() == GoodsTypeEnum.EXCLUSIVE.getValue()) {
				BigDecimal firstDecimal = bean.getRental()
						.divide(new BigDecimal(bean.getUseVehicleTime()), 2, BigDecimal.ROUND_HALF_DOWN)
						.stripTrailingZeros();
				beanDto.setRental(bean.getRental());
				beanDto.setRentalUnitPrice("(" + firstDecimal.toPlainString() + "元 x " + bean.getUseVehicleTime() + "期)");
				beanDto.setCurrentAmount(firstDecimal);
				beanDto.setSurplusAmount(bean.getRental().subtract(firstDecimal));
				beanDto.setSurplusNumber((bean.getUseVehicleTime() - 1) + "");
				if (GoodsPaymentTypeEnum.WHOLE_PARAGRAPH.getValue() == bean.getPaymentType()) {
					beanDto.setTotalAmount(bean.getRental().add(bean.getBond()));
				} else {
					beanDto.setTotalAmount(firstDecimal.add(bean.getBond()));
				}
			} else {
				beanDto.setRental(bean.getRental());
				beanDto.setTotalAmount(bean.getRental().add(bean.getBaseMileagePrice())
						.add(bean.getPackageMileagePrice()).add(bean.getBond()));
			}
			String startTime = DateUtil.formatDate(LocalDate.now()) + "(周" + DateUtil.getWeek(LocalDateTime.now())
					+ ")";
			beanDto.setStartTime(startTime);
			LocalDate endDate = LocalDate.now();
			endDate = endDate.plusMonths(bean.getUseVehicleTime());
			String endTime = DateUtil.formatDate(endDate) + "(周" + DateUtil.getWeek(endDate) + ")";
			beanDto.setEndTime(endTime);
		}
		return beanDto;
	}

	public static void main(String[] args) {
		SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
		String startTime = DateUtil.formatDate(LocalDate.now()) + "(" + dateFm.format(new Date()) + ")";
		System.out.println(startTime);
		LocalDate endDate = LocalDate.now();
		endDate = endDate.plusMonths(1);
		String endTime = DateUtil.formatDate(endDate) + "(" + dateFm.format(new Date()) + ")";
		System.out.println(endTime);
	}

	private static void composeAppGoods(GoodsAppDetailVo goodsDto) {
		if (goodsDto.getGoodsType() == GoodsTypeEnum.EXCLUSIVE.getValue()) {
			goodsDto.setMonthlySupply(goodsDto.getRental()
					.divide(new BigDecimal(goodsDto.getUseVehicleTime()), 2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString());
		} else {
			BigDecimal result = (goodsDto.getBaseMileagePrice().add(goodsDto.getPackageMileagePrice()).divide(
					new BigDecimal(goodsDto.getBaseMileage() + goodsDto.getPackageMileage()), 2, RoundingMode.HALF_UP));
			goodsDto.setMileagePrice(result.stripTrailingZeros().toPlainString());
		}

	}

	public static GoodsExclusiveDrawerVo toGoodsExclusiveDrawerVo(GoodsExclusiveDrawerBizDto bean) {
		GoodsExclusiveDrawerVo beanDto = null;
		if (bean != null) {
			beanDto = new GoodsExclusiveDrawerVo();
			BeanUtils.copyProperties(bean, beanDto);
		}
		return beanDto;
	}

	public static GoodsSubscribeDrawerVo toGoodsSubscribeDrawerVo(GoodsSubscribeDrawerBizDto bean) {
		GoodsSubscribeDrawerVo beanDto = null;
		if (bean != null) {
			beanDto = new GoodsSubscribeDrawerVo();
			BeanUtils.copyProperties(bean, beanDto);
		}
		return beanDto;
	}

	public static List<GoodsDrawerMileageVo> toGoodsDrawerMileageVo(List<GoodsReletMileageBizDto> beanBizDtos) {
		List<GoodsDrawerMileageVo> beanVos = null;
		if (beanBizDtos != null && !beanBizDtos.isEmpty()) {
			beanVos = new ArrayList<>();
			for (GoodsReletMileageBizDto beanBizDto : beanBizDtos) {
				beanVos.add(toGoodsDrawerMileageVo(beanBizDto));
			}
		}
		return beanVos;
	}

	public static GoodsDrawerMileageVo toGoodsDrawerMileageVo(GoodsReletMileageBizDto beanBizDto) {
		GoodsDrawerMileageVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new GoodsDrawerMileageVo();
			BeanUtils.copyProperties(beanBizDto, beanVo);
			beanVo.setUseVehicleTime(beanBizDto.getUseVehicleTime());
			beanVo.setBasePrice(beanBizDto.getBaseMileagePrice());
			beanVo.setRental(beanBizDto.getRental());
			beanVo.setPackageMileageList(toGoodsDrawerPackageVo(beanBizDto.getPackageList()));
		}
		return beanVo;
	}

	public static List<GoodsDrawerPackageVo> toGoodsDrawerPackageVo(List<GoodsMileagePackageReletBizDto> beanBizDtos) {
		List<GoodsDrawerPackageVo> beanVos = null;
		if (beanBizDtos != null && !beanBizDtos.isEmpty()) {
			beanVos = new ArrayList<>();
			for (GoodsMileagePackageReletBizDto beanBizDto : beanBizDtos) {
				beanVos.add(toGoodsDrawerPackageVo(beanBizDto));
			}
		}
		return beanVos;
	}

	public static GoodsDrawerPackageVo toGoodsDrawerPackageVo(GoodsMileagePackageReletBizDto beanBizDto) {
		GoodsDrawerPackageVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new GoodsDrawerPackageVo();
			// 里程包ID
			beanVo.setPackageMileageId(beanBizDto.getPackageId());
			// 里程包里程数
			beanVo.setPackageMileage(beanBizDto.getMiles());
			// 里程包里程价格
			beanVo.setPackageMileagePrice(beanBizDto.getFee());
			// 赠送里程
			beanVo.setGiftMileage(beanBizDto.getGiftMiles());
		}
		return beanVo;
	}

	public static GoodsConfirmVo toGoodsConfirmVo(OrderSubscribeBizDto orderSubscribeBizDto, GoodsOrderBizDto goods,
			OrderCreateTypeEnum type) {
		GoodsConfirmVo beanVo = new GoodsConfirmVo();
		// 订单号(原订单)
		beanVo.setOrderCode(orderSubscribeBizDto.getOrderCode());
		// 下单门店id
		beanVo.setStoreId(orderSubscribeBizDto.getPlaceStoreId());
		// 规格信息
		beanVo.setSpecific(
				goods.getUseVehicleTime() + "个月/" + goods.getBaseMileage() + "公里," + goods.getPackageMileage() + "公里");
		// 赠送里程
		beanVo.setGiftMileage(goods.getGiftMileage());
		if (goods.getGiftMileage() == null || goods.getGiftMileage() == 0) {
			beanVo.setGiftMileageStr("--");
		} else {
			beanVo.setGiftMileageStr(goods.getGiftMileage() + "公里");
		}
		// 合计金额
		BigDecimal totalFee = BigDecimal.ZERO;
		if (OrderCreateTypeEnum.RENEW.equals(type)) {
			// 续订
			// 续订租期说明
			LocalDate leaseInception = orderSubscribeBizDto.getLeaseExpire().plus(1L, ChronoUnit.DAYS);
			LocalDate leaseExpire = leaseInception.plusMonths(goods.getUseVehicleTime().longValue());
			beanVo.setRenewExplain("续订" + goods.getUseVehicleTime() + "个月：" + DateUtil.formatDate(leaseInception)
					+ " (周" + DateUtil.getWeek(leaseInception) + ")至" + DateUtil.formatDate(leaseExpire) + " (周"
					+ DateUtil.getWeek(leaseExpire) + ")");
			// 费用明细
			List<CostDetailsVo> costs = new ArrayList<>();
			costs.add(new CostDetailsVo("租车费", goods.getUseVehicleTime() + "个月", goods.getRental()));
			costs.add(new CostDetailsVo("基础里程费", goods.getBaseMileage() + "公里", goods.getBaseMileagePrice()));
			costs.add(new CostDetailsVo("订阅里程费", goods.getPackageMileage() + "公里", goods.getPackageMileagePrice()));
			if (goods.getGiftMileage() != null && goods.getGiftMileage() > 0) {
				// 赠送里程
				Integer giftFee = goods.getGiftMileage() * OrderConst.GIFT_UNIT_PRICE;
				CostDetailsVo costDetails = new CostDetailsVo("赠送里程", goods.getGiftMileage() + "公里",
						new BigDecimal(giftFee));
				costDetails.setAmountStr("价值" + costDetails.getAmountStr());
				costs.add(costDetails);
			}
			beanVo.setCosts(costs);
			totalFee = totalFee.add(goods.getRental()).add(goods.getBaseMileagePrice())
					.add(goods.getPackageMileagePrice());
		} else if (OrderCreateTypeEnum.PURCHASED.equals(type)) {
			// 加购
			// 加购说明
			beanVo.setPurchaseExplains(Arrays.asList(new DataVo("订阅里程", goods.getPackageMileage() + "公里"),
					new DataVo("使用期限", "有效期至 " + DateUtil.formatDate(orderSubscribeBizDto.getLeaseExpire()))));
			// 费用明细
			List<CostDetailsVo> costs = new ArrayList<>();
			costs.add(new CostDetailsVo("订阅里程费", goods.getPackageMileagePrice()));
//			if (orderSubscribeBizDto.getBeyondMileage() != null) {
//				costs.add(new CostDetailsVo("超里程费用",
//						orderSubscribeBizDto.getBeyondMileage() + "公里 * " + OrderConst.MILEAGE_UNIT_PRICE + "元/公里",
//						orderSubscribeBizDto.getBeyondFee(), true));
//				totalFee = totalFee.add(orderSubscribeBizDto.getBeyondFee());
//			}
			beanVo.setCosts(costs);

			totalFee = totalFee.add(goods.getPackageMileagePrice());
		}

		beanVo.setTotalFee(totalFee);

		return beanVo;
	}

	public static List<VehicleMakeVo> toVehicleMakeVo(List<VehicleMakeDto> beanBizDtos) {
		List<VehicleMakeVo> beanVos = null;
		if (beanBizDtos != null && !beanBizDtos.isEmpty()) {
			beanVos = new ArrayList<>();
			for (VehicleMakeDto beanBizDto : beanBizDtos) {
				beanVos.add(toVehicleMakeVo(beanBizDto));
			}
		}
		return beanVos;
	}

	public static VehicleMakeVo toVehicleMakeVo(VehicleMakeDto beanBizDto) {
		VehicleMakeVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new VehicleMakeVo();
			BeanUtils.copyProperties(beanBizDto, beanVo);
		}
		return beanVo;
	}

	public static List<GoodsIntroduceVo> toGoodsIntroduceVo(List<GoodsIntroduceDto> beanBizDtos) {
		List<GoodsIntroduceVo> beanVos = null;
		if (beanBizDtos != null && !beanBizDtos.isEmpty()) {
			beanVos = new ArrayList<>();
			for (GoodsIntroduceDto beanBizDto : beanBizDtos) {
				GoodsIntroduceVo beanVo = new GoodsIntroduceVo();
				BeanUtils.copyProperties(beanBizDto, beanVo);
				beanVos.add(beanVo);
			}
		}
		return beanVos;
	}

	public static List<GoodsIntroduceVo> toGoodsIntroduceVo(List<GoodsIntroduceDto> beanBizDtos,
															GoodsAppDetailVo goodsAppDetailVo) {
		List<GoodsIntroduceVo> beanVos = null;
		if (beanBizDtos != null && !beanBizDtos.isEmpty()) {
			beanVos = new ArrayList<>();
			List<GoodsIntroduceVo> introduceImgList = new ArrayList<>();
			List<GoodsIntroduceVo> introduceQuestionList = new ArrayList<>();
			for (GoodsIntroduceDto beanBizDto : beanBizDtos) {
				if (beanBizDto.getType() == 1) {
					introduceImgList.add(toGoodsIntroduceVo(beanBizDto, goodsAppDetailVo));
				} else {
					introduceQuestionList.add(toGoodsIntroduceVo(beanBizDto, goodsAppDetailVo));
				}
			}
			goodsAppDetailVo.setIntroduceImgList(introduceImgList);
			goodsAppDetailVo.setIntroduceQuestionList(introduceQuestionList);
		}
		return beanVos;
	}

	public static GoodsIntroduceVo toGoodsIntroduceVo(GoodsIntroduceDto beanBizDto, GoodsAppDetailVo goodsAppDetailVo) {
		GoodsIntroduceVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new GoodsIntroduceVo();
			BeanUtils.copyProperties(beanBizDto, beanVo);
		}
		return beanVo;
	}

	public static List<GoodsBigScreenVo> toGoodsBigScreenVo(List<GoodsBigScreenBizDto> beanBizDtos) {
		List<GoodsBigScreenVo> beanVos = null;
		if (beanBizDtos != null && !beanBizDtos.isEmpty()) {
			beanVos = new ArrayList<>();
			for (GoodsBigScreenBizDto beanBizDto : beanBizDtos) {
				beanVos.add(toGoodsBigScreenVo(beanBizDto));
			}
		}
		return beanVos;
	}

	public static GoodsBigScreenVo toGoodsBigScreenVo(GoodsBigScreenBizDto beanBizDto) {
		GoodsBigScreenVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new GoodsBigScreenVo();
			BeanUtils.copyProperties(beanBizDto, beanVo);
			beanVo.setSpecList(toGoodsBigScreenDrawerSpecVo(beanBizDto.getSpecList()));
			beanVo.setMileageList(toGoodsBigScreenDrawerMileageVo(beanBizDto.getMileageList()));
		}
		return beanVo;
	}

	public static List<GoodsBigScreenDrawerSpecVo> toGoodsBigScreenDrawerSpecVo(
			List<GoodsBigScreenDrawerSpecBizDto> beanBizDtos) {
		List<GoodsBigScreenDrawerSpecVo> beanVos = null;
		if (beanBizDtos != null && !beanBizDtos.isEmpty()) {
			beanVos = new ArrayList<>();
			for (GoodsBigScreenDrawerSpecBizDto beanBizDto : beanBizDtos) {
				beanVos.add(toGoodsBigScreenDrawerSpecVo(beanBizDto));
			}
		}
		return beanVos;
	}

	public static GoodsBigScreenDrawerSpecVo toGoodsBigScreenDrawerSpecVo(GoodsBigScreenDrawerSpecBizDto beanBizDto) {
		GoodsBigScreenDrawerSpecVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new GoodsBigScreenDrawerSpecVo();
			BeanUtils.copyProperties(beanBizDto, beanVo);
		}
		return beanVo;
	}

	public static List<GoodsBigScreenDrawerMileageVo> toGoodsBigScreenDrawerMileageVo(
			List<GoodsBigScreenDrawerMileageBizDto> beanBizDtos) {
		List<GoodsBigScreenDrawerMileageVo> beanVos = null;
		if (beanBizDtos != null && !beanBizDtos.isEmpty()) {
			beanVos = new ArrayList<>();
			for (GoodsBigScreenDrawerMileageBizDto beanBizDto : beanBizDtos) {
				beanVos.add(toGoodsBigScreenDrawerMileageVo(beanBizDto));
			}
		}
		return beanVos;
	}

	public static GoodsBigScreenDrawerMileageVo toGoodsBigScreenDrawerMileageVo(
			GoodsBigScreenDrawerMileageBizDto beanBizDto) {
		GoodsBigScreenDrawerMileageVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new GoodsBigScreenDrawerMileageVo();
			BeanUtils.copyProperties(beanBizDto, beanVo);
			beanVo.setPackageMileageList(toGoodsBigScreenDrawerPackageVo(beanBizDto.getPackageMileageList()));
		}
		return beanVo;
	}

	public static List<GoodsBigScreenDrawerPackageVo> toGoodsBigScreenDrawerPackageVo(
			List<GoodsBigScreenDrawerPackageDto> beanBizDtos) {
		List<GoodsBigScreenDrawerPackageVo> beanVos = null;
		if (beanBizDtos != null && !beanBizDtos.isEmpty()) {
			beanVos = new ArrayList<>();
			for (GoodsBigScreenDrawerPackageDto beanBizDto : beanBizDtos) {
				beanVos.add(toGoodsBigScreenDrawerPackageVo(beanBizDto));
			}
		}
		return beanVos;
	}

	public static GoodsBigScreenDrawerPackageVo toGoodsBigScreenDrawerPackageVo(
			GoodsBigScreenDrawerPackageDto beanBizDto) {
		GoodsBigScreenDrawerPackageVo beanVo = null;
		if (beanBizDto != null) {
			beanVo = new GoodsBigScreenDrawerPackageVo();
			BeanUtils.copyProperties(beanBizDto, beanVo);
		}
		return beanVo;
	}

	/********************************************
	 * 商品信息转换 结束
	 *****************************/

}
